package com.archway.appframe.task;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.CronTriggerBean;

@Configuration
public class AppframeTaskConfig {

	@Bean
	public SchedulerFactoryBean SpringJobSchedulerFactoryBean() throws ParseException{
		
		SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
		schedulerFactory.setTaskExecutor(this.taskExecuteConfig());//任务执行配置
		schedulerFactory.setTriggers(this.triggerConfig());//任务触发器
		return schedulerFactory;
	}
	
	private Trigger[] triggerConfig() throws ParseException {
		List<Trigger> TriggerList= new ArrayList<Trigger>();
		CronTriggerBean cornTriggerOne = new CronTriggerBean();
		cornTriggerOne.setName("name");
//		cornTriggerOne.setJobGroup("jobgroup");
//		cornTriggerOne.setJobName("jobname");
		cornTriggerOne.setGroup("group");
		cornTriggerOne.setCronExpression("0/5 * * * * ?");
		JobDetail jobDetail = new JobDetail();
		jobDetail.setJobClass(OrginTask.class);
		jobDetail.setName("a job");
		jobDetail.setGroup("g1");
		cornTriggerOne.setJobDetail(jobDetail);
		TriggerList.add(cornTriggerOne);
		
		return TriggerList.toArray(new Trigger[0]);
	}

	/**
	 * 
	 * <strong>(please edit it when context changed)</strong></br>
	 * <h1>描述:</h1>
	 * <li>线程执行器配置，用于任务注册</li> 
	 * <li>描述细节列表</li>  
	 * @return
	 * @author lirn
	 * @createDate 2014年5月15日 下午4:00:14
	 */
	private ThreadPoolTaskExecutor taskExecuteConfig(){
//		corePoolSize： 线程池维护线程的最少数量 
//		keepAliveSeconds 线程池维护线程所允许的空闲时间 
//		maxPoolSize 线程池维护线程的最大数量 
//		queueCapacity 线程池所使用的缓冲队列 -web程序内不建议使用过多任务
		ThreadPoolTaskExecutor executor= new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(3);
		executor.setMaxPoolSize(10);
		executor.setQueueCapacity(5);
		executor.setKeepAliveSeconds(60);
		//executor.setWaitForTasksToCompleteOnShutdown(true);
		return executor;
	}
	
}
